package com.example.myFirstWebmagic.waterReport;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 
 * ClassName: MysqlPipeline 
 * @Description: 爬虫结果保存到数据库
 * @author xiongyao
 * @date 2017年2月28日
 */

@Transactional
public class WaterStationMysqlPipeline implements Pipeline {
	
	private final WaterStationReportRepository repository;
	
	public WaterStationMysqlPipeline(WaterStationReportRepository repository){
		this.repository=repository;
	}
	
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		// TODO Auto-generated method stub
        //遍历所有结果，保存到数据库
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            @SuppressWarnings("unchecked")
			List<EnWaterStationReport> list= (List<EnWaterStationReport>) entry.getValue();
            for(EnWaterStationReport report:list){
            	repository.save(report);
            }
        }

	}

}
